home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / apps / 42 / wind6.prf < prev    next >
Text File  |  1986-07-17  |  21KB  |  417 lines

  1. .!****************************************************************************
  2. .! 
  3. .! ANTIC PUBLISHING INC., COPYRIGHT 1985.  REPRINTED BY PERMISSION.
  4. .!
  5. .! ** Professional GEM ** by Tim Oren
  6. .!
  7. .! Proff File by ST enthusiasts at
  8. .! Case Western Reserve University
  9. .! Cleveland, Ohio
  10. .! uucp : decvax!cwruecmp!bammi
  11. .! csnet: bammi@case
  12. .! arpa : bammi%case@csnet-relay
  13. .! compuserve: 71515,155
  14. .!
  15. .!****************************************************************************
  16. .!
  17. .!
  18. .!****************************************************************************
  19. .!
  20. .!            Begin Part 6
  21. .!
  22. .!****************************************************************************
  23. .!
  24. .PART VI Raster operations
  25. .SH SEASONS GREETINGS
  26. This is the Yuletide installment of ST PRO GEM, devoted to
  27. explaining the raster, or "bit-blit" portion of the Atari ST's VDI
  28. functions.
  29. .PP
  30. Please note that this is NOT an attempt to show how to write
  31. directly to the video memory, although you will be able to deduce a
  32. great deal from the discussion.
  33. .PP
  34. As usual, there is a download with this column.  You will find
  35. it in ATARI16 (PCS-58) in DL3 under the name of GEMCL6.C.
  36. .SH DEFINING TERMS
  37. To understand VDI raster operations, you need  to understand the
  38. jargon used to describe them.  (Many programmers will be tempted to
  39. skip this section and go directly to the code.  Please don't do it
  40. this time:  Learning the jargon is the larger half of understanding
  41. the raster operations!)
  42. .PP
  43. In VDI terms a raster area is simply a chunk of contiguous words
  44. of memory, defining a bit image.  This chunk is called a "form".  A
  45. form  may reside in the ST's video map area or it may be in the data
  46. area of  your application.  Forms are roughly analogous to "blits" or
  47. "sprites"  on other systems.  (Note, however, that there is no sprite
  48. hardware on the ST.)
  49. .PP
  50. Unlike other systems, there is NO predefined organization of the
  51. raster form.  Instead, you determine the internal layout of the form
  52. with an auxiliary data structure called the MFDB, or Memory Form
  53. Definition Block.  Before going into the details of the MFDB, we need
  54. to look at the various format options.  Their distinguishing features
  55. are monochrome vs. color, standard vs. device-specific and even-word
  56. vs. fringed.
  57. .SH MONOCHROME VS. COLOR
  58. Although these terms are standard, it might be better to say
  59. "single-color vs. multi-color".  What we are actually defining is the
  60. number of bits which correspond to each dot, or pixel, on the screen.
  61. In the ST, there are three possible answers. The high-resolution mode
  62. has one bit per pixel, because there is only one "color": white.
  63. .PP
  64. In the medium resolution color mode, there are four possible
  65. colors for each pixel.  Therefore, it takes two bits to represent
  66. each dot on the screen.  (The actual colors which appear are
  67. determined by  the settings of the ST's pallette registers.)
  68. .PP
  69. In the low resolution color mode, sixteen colors are generated
  70. requiring four bits per pixel.  Notice that as the number of bits per
  71. pixel has been doubled for each mode, so the number of pixels on the
  72. screen has been halved: 640 by 400 for monochrome, 640 by 200 for
  73. medium-res, and 320 by 200 by low-res.  In this way the ST always
  74. uses  the same amount of video RAM: 32K.
  75. .PP
  76. Now we have determined how many bits are needed for each pixel,
  77. but not how they are laid out within the form.  To find this out, we
  78. have  to see whether the form is device-dependent or not.
  79. .SH STANDARD VS. DEVICE-SPECIFIC FORMAT
  80. The standard raster form format is a constant layout which is
  81. the same for all GEM systems.  A device-specific form is one which is
  82. stored in the internal format of a particular GEM system.  Just as
  83. the ST has three different screen modes, so it has three different
  84. device-specific form formats.  We will look at standard form first,
  85. then the ST-specific forms.
  86. .PP
  87. First, it's reasonable to ask why a standard format is used. Its
  88. main function is to establish a portability method between various
  89. GEM systems.  For instance, an icon created in standard format on an
  90. IBM PC GEM setup can be moved to the ST, or a GEM Paint picture from
  91. an AT&T 6300 could be loaded into the ST version of Paint.
  92. .PP
  93. The standard format has some uses even if you only work with the
  94. ST, because it gives a method of moving your application's icons and
  95. images amongst the three different screen modes.  To be sure,  there
  96. are limits to this.  Since there are different numbers of pixels  in
  97. the different modes, an icon built in the high-resolution mode will
  98. appear twice as large in low-res mode, and would appear oblong in
  99. medium-res.  (You can see this effect in the ST Desktop's icons.)
  100. Also,  colors defined in the lower resolutions will be useless in
  101. monochrome.
  102. .PP
  103. The standard monochrome format uses a one-bit to represent
  104. black, and uses a zero for white.  It is assumed that the form begins
  105. at the  upper left of the raster area, and is written a word at a
  106. time left to right on each row, with the rows being output top to
  107. bottom.  Within each word, the most significant bit is the left-most
  108. on the screen.
  109. .PP
  110. The standard color form uses a storage method called "color
  111. planes". The high-order bits for all of the pixels are stored just as
  112. for monochrome, followed by the next-lowest bit in another contiguous
  113. block, and so on until all of the necessary color bits have been
  114. stored.
  115. .PP
  116. For example, on a 16-color system, there would be four different
  117. planes.  The color of the upper-leftmost bit in the form would be
  118. determined by concatenating the high-order bit in the first word of
  119. each plane of the form.
  120. .PP
  121. The system dependent form for the ST's monochrome mode is very
  122. simple: it is identical to the standard form!  This occurs because
  123. the  ST uses a "reverse-video" setup in monochrome mode, with the
  124. background  set to white.
  125. .PP
  126. The video organization of the ST's color modes is more
  127. complicated. It uses an "interleaved plane" system to store the bits
  128. which make up a pixel.  In the low-resolution mode, every four words
  129. define the values of 16 pixels.  The high-order bits of the four
  130. words are merged to form the left-most pixel, followed by the next
  131. lower bit of each word, and so on.  This method is called
  132. interleaving because the usually separate color planes described
  133. above have been shuffled together in memory.
  134. .PP
  135. The organization of the ST's medium-resolution mode is similar
  136. to low-res, except the only two words are taken at a time.  These are
  137. merged to create the two bits needed to address four colors.
  138. .PP
  139. You should note that the actual color produced by a particular
  140. pixel value is NOT fixed.  The ST uses a color remapping system
  141. called a palette.  The pixel value in memory is used to address a
  142. hardware  register in the palette which contains the actual RGB
  143. levels to be sent to the display.  Programs may set the palette
  144. registers with  BIOS calls, or the user may alter its settings with
  145. the Control Panel desk accessory.  Generally, palette zero
  146. (background) is left as white, and the highest numbered palette is
  147. black.
  148. .SH EVEN-WORD VS. FRINGES
  149. A form always begins on a word boundary, and is always stored
  150. with an integral number of words per row.  However,  it is possible
  151. to use only a portion of the final word.  This partial word is called
  152. a "fringe".  If, for instance, you had a form 40 pixels wide,  it
  153. would be stored with four words per row: three whole words, and one
  154. word with the eight pixel fringe in its upper byte.
  155. .SH MFDB's
  156. Now we can intelligently define the elements of the MFDB.  Its
  157. exact C structure definition will be found in the download. The
  158. fd_nplanes entry determines the color scheme: a value of one is
  159. monochrome, more than one denotes a color form.  If fd_stand is zero,
  160. then the form is device-specific, otherwise it is in standard format.
  161. .PP
  162. The fd_w and fd_h fields contain the pixel width and height of
  163. the form respectively.  Fd_wdwidth is the width of a row in words.
  164. If fd_w is not exactly equal to sixteen times fd_wdwidth, then the
  165. form has a fringe.
  166. .PP
  167. Finally, fd_addr is the 32-bit memory address of the form
  168. itself. Zero is a special value for fd_addr.  It denotes that this
  169. MFDB is for the video memory itself.  In this case, the VDI
  170. substitutes the actual address of the screen, and it ignores ALL of
  171. the other parameters. They are replaced with the size of the whole
  172. screen and number of planes in the current mode, and the form is (of
  173. course) in device-specific format.
  174. .PP
  175. This implies that any MFDB which points at the screen can only
  176. address the entire screen.  This is not a problem, however, since the
  177. the VDI raster calls allow you to select a rectangular region within
  178. the form.  (A note to advanced programmers:  If this situation is
  179. annoying, you can retrieve the address of the ST's video area from
  180. low memory, add an appropriate offset, and substitute it into the
  181. MFDB yourself to address a portion of the screen.)
  182. .SH LET'S OPERATE
  183. Now we can look at the VDI raster operations themselves.  There
  184. are actually three: transform form, copy raster opaque, and copy
  185. raster transparent.  Both copy raster functions can perform a variety
  186. of logic operatoins during the copy.
  187. .SH TRANSFORM FORM
  188. The purpose of this operation is to change the format of a form:
  189. from standard to device-specific, or vice-versa.  The calling
  190. sequence is:
  191. .FB vr_trnfm()
  192. vr_trnfm(vdi_handle, source, dest);
  193. .FE
  194. where source and dest are each pointers to MFDBs.  They ARE allowed
  195. to be the same.  Transform form checks the fd_stand flag in the
  196. source MFDB, toggles it and writes it into the destination MFDB after
  197. rewriting the form itself.  Note that transform form CANNOT change
  198. the number of color planes in a form: fd_nplanes must be identical in
  199. the two MFDBs.
  200. .PP
  201. If you are writing an application to run on the ST only, you
  202. will probably be able to avoid transform form entirely.  Images and
  203. icons are stored within resources as standard forms, but since they
  204. are monochrome, they will work "as is" with the ST.
  205. .PP
  206. If you may want to move your program or picture files to another
  207. GEM system, then you will need transform form.  Screen images can be
  208. transformed to standard format and stored to disk.  Another system
  209. with the same number of color planes could the read the files, and
  210. transform the image to ITS internal format with transform form.
  211. .PP
  212. A GEM application which will be moved to other systems needs to
  213. contain code to transform the images and icons within its resource,
  214. since standard and device-specific formats will not always coincide.
  215. .PP
  216. If you are in this situation, you will find several utilities in
  217. the download which you can use to transform G_ICON and G_IMAGE
  218. objects. There is also a routine which may be used with map_tree()
  219. from the  last column in order to transform all of the images and
  220. icons in a resource tree at once.
  221. .SH COPY RASTER OPAQUE
  222. This operation copies all or part of the source form into the
  223. destination form.  Both the source and destination forms must be in
  224. device-specific form.  Copy raster opaque is for moving information
  225. between "like" forms, that is, it can copy from monochrome to
  226. monochrome, or between color forms with the same number of planes.
  227. The calling format is:
  228. .FB vro_cpyfm()
  229. vro_cpyfm(vdi_handle, mode, pxy, source, dest);
  230. .FE
  231. As above, the source and dest parameters are pointers to MFDBs
  232. (which in turn point to the actual forms).  The two MFDBs may point
  233. to memory areas  which overlap.  In this case, the VDI will perform
  234. the move in a  non-destructive order.  Mode determines how the pixel
  235. values in the source and destination areas will be combined.  I will
  236. discuss it separately later on.
  237. .PP
  238. The pxy parameter is a pointer to an eight-word integer array.
  239. This array defines the area within each form which will be affected.
  240. Pxy[0] and pxy[1] contain, respectively, the X and Y coordinates of
  241. the upper left corner of the source rectangle.  These are given as
  242. positive  pixel displacements from the upper left of the form.
  243. Pxy[2] and pxy[3]  contain the X and Y displacements for the lower
  244. right of the source rectangle.
  245. .PP
  246. Pxy[4] through pxy[7] contain the destination rectangle in the
  247. same format.  Normally, the destination and source should be the same
  248. size.  If not, the size given for the source rules, and the whole are
  249. is transferred beginning at the upper left given for the destination.
  250. .PP
  251. This all sounds complex, but is quite simple in many cases.
  252. Consider an example where you want to move a 32 by 32 pixel area
  253. from one part of the display to another.  You would need to allocate
  254. only one MFDB, with a zero in the fd_addr field.  The VDI will take
  255. care of counting color planes and so on.  The upper left raster
  256. coordinates of the source and destination rectangles go into pxy[0],
  257. pxy[1] and pxy[4], pxy[5] respectively.  You add 32 to each of these
  258. values and insert the results in the corresponding lower right
  259. entries, then make the copy call using the same MFDB for both source
  260. and destination.  The VDI takes care of any overlaps.
  261. .SH COPY RASTER TRANSPARENT
  262. This operation is used for copying from a monochrome form to a
  263. color form.  It is called transparent because it "writes through" to
  264. all of the color planes.  Again, the forms need to be in
  265. device-specific form.  The calling format is:
  266. .FB vrt_cpyfm()
  267. vrt_cpyfm(vdi_handle, mode, pxy, source, dest, color);
  268. .FE
  269. All of the parameters are the same as copy opaque, except that
  270. color has been added.  Color is a pointer to a two word integer
  271. array. Color[0] contains the color index which will be used when a
  272. one appears in the source form, and color[1] contains the index for
  273. use when a zero occurs.
  274. .PP
  275. Incidentally, copy transparent is used by the AES to draw
  276. G_ICONs and G_IMAGEs onto the screen.  This explains why you do not
  277. need to convert them to color forms yourself.
  278. .PP
  279. A note for advanced VDI programmers: The pxy parameter in both
  280. copy opaque and transparent may be given in normalized device
  281. coordinates (NDC) if the workstation associated with vdi_handle was
  282. opened for NDC work.
  283. .SH THE MODE PARAMETER
  284. The mode variable used in both of the copy functions is an
  285. integer with a value between zero and fifteen. It is used to select
  286. how the copy function will merge the pixel values of the source and
  287. destination forms.  The complete table of functions is given in the
  288. download.  Since a number of these are of obscure or questionable
  289. usefulness, I will only discuss the most commonly used modes.
  290. .SH REPLACE MODE
  291. A mode of 3 results in a straight-forward copy: every
  292. destination pixel is replaced with the corresponding source form
  293. value.
  294. .SH ERASE MODE
  295. A mode value of 4 will erase every destination pixel which
  296. corresponds to a one in the source form.  (This mode corresponds to
  297. the "eraser" in a Paint program.)  A mode value of 1 will erase every
  298. destination pixel which DOES NOT correspond to a one in the source.
  299. .SH XOR MODE
  300. A mode value of 6 will cause the destination pixel to be toggled
  301. if the corresponding source bit is a one. This operation is
  302. invertable, that is, executing it again will reverse the effects.
  303. For this reason it is often used for "software sprites" which must be
  304. shown and then removed from the screens.  There are some problems
  305. with this in color operations, though - see below.
  306. .SH TRANSPARENT MODE
  307. Don't confuse this term with the copy  transparent function
  308. itself.  In this case it simply means that ONLY  those destination
  309. pixels corresponding with ones in the source form will  be modified
  310. by the operation.  If a copy transparent is being performed, the
  311. value of color[0] is substituted for each one bit in the source form.
  312. A mode value of 7 selects transparent mode.
  313. .SH REVERSE TRANSPARENT MODE
  314. This is like transparent mode except that only those destination
  315. pixels corresponding to source ZEROS are modified.  In a copy
  316. transparent, the value of color[1] is substituted for each zero bit.
  317. Mode 13 selects reverse transparent.
  318. .SH THE PROBLEM OF COLOR
  319. I have discussed the various modes as if they deal with one and
  320. zero pixel values only.  This is exactly true when both forms are
  321. monochrome, but is more complex when one or both are color forms.
  322. .PP
  323. When both forms are color, indicating that a copy opaque is
  324. being performed, then the color planes are combined bit-by-bit using
  325. the rule for that mode.  That is, for each corresponding source and
  326. destination pixel, the VDI extracts the top order bits and processes
  327. them, then operates on the next lower bit, and so on, stuffing each
  328. bit back into the destination form as the copy progresses.  For
  329. example, an XOR operation on pixels valued 7 and 10 would result in a
  330. pixel value of 13.
  331. .PP
  332. In the case of a copy transparent, the situation is more
  333. complex. The source form consists of one plane, and the destination
  334. form has two  or more.  In order to match these up, the color[] array
  335. is used.  Whenever a one pixel is found, the value of color[0] is
  336. extracted and used in the bit-by-bit merge process described in the
  337. last paragraph.  When a zero is found, the value of color[1] is
  338. merged into the destination form.
  339. .PP
  340. As you can probably see, a raster copy using a mode which
  341. combines the source and destination can be quite complex when color
  342. planes are used!  The situation is compounded on the ST, since the
  343. actual color values may be remapped by the palette at any time.   In
  344. many cases, just using black and white in color[] may achieve the
  345. effects you desire.  If need to use full color, experimentation is
  346. the  best guide to what looks good on the screen and what is garish
  347. or  illegible.
  348. .SH OPTIMIZING RASTER OPERATIONS
  349. Because the VDI raster functions are extremely generalized, they
  350. are also slower than hand-coded screen drivers which you might write
  351. for your own special cases.  If you want to speed up your
  352. application's raster operations without writing assembl language
  353. drivers, the following hints will help you increase the VDI's
  354. performance.
  355. .SH AVOID MERGED COPIES
  356. These are copy modes, such as XOR,  which  require that words be
  357. read from the destination form.  This extra memory access increases
  358. the running time by up to fifty percent.
  359. .SH MOVE TO CORRESPONDING PIXELS
  360. The bit position within a word of the destination rectangle
  361. should correspond with the bit position of the source rectangle's
  362. left edge.  For instance, if the source's left edge is one pixel in,
  363. then the destination's edge could be at one, seventeen, thirty-three,
  364. and so.  Copies which do not obey this rule force the VDI to shift
  365. each word of the form as it is moved.
  366. .SH AVOID FRINGES
  367. Put the left edge of the source and destination  rectangles on
  368. an even word boundary, and make their widths even multiples  of
  369. sixteen.  The VDI then does not have to load and modify partial words
  370. within the destination forms.
  371. .SH USE ANOTHER METHOD
  372. Sometimes a raster operation is not the fastest way to
  373. accomplish your task.  For instance, filling a rectangle with zeros
  374. or ones may be accomplished by using raster copy modes zero and
  375. fifteen, but it is faster to use the VDI v_bar function instead.
  376. Likewise, inverting an area on the screen may be done more quickly
  377. with v_bar by using BLACK in XOR mode.  Unfortunately, v_bar cannot
  378. affect  memory which is not in the video map, so these alternatives
  379. do not always work.
  380. .SH  FEEDBACK RESULTS
  381. The results of the poll on keeping or  dropping the use of
  382. portability macros are in.  By a slim margin, you have voted to keep
  383. them.  The vote was close enough that in future columns I will try to
  384. include ST-only versions of routines which make  heavy use of the
  385. macros.  C purists and dedicated Atarians may then use the alternate
  386. code.
  387. .SH  THE NEXT QUESTION
  388. This time I'd like to ask you to drop by the Feedback Section
  389. and tell me whether the technical level of the columns has been:
  390. .br
  391. .sp 1
  392. .in +4
  393. A)  Too hard!  Who do you think we are, anyway?
  394. .br
  395. B)  Too easy!  Don't underestimate Atarians.
  396. .br
  397. C)  About right, on the average.
  398. .in -4
  399. .PP
  400. If you have the time, it would also help to know a little about
  401. your background, for instance, whether you are a professional
  402. programmer, how long you have been computing, if you owned an 8-bit
  403. Atari, and so on.
  404. .SH COMING UP SOON
  405. The next column will deal with GEM menus: How they are
  406. constructed, how to decipher menu messages, and how to change menu
  407. entries at run-time.  The following issue will contain more feedback
  408. response, and a discussion on designing user interfaces for GEM
  409. programs.
  410. .!
  411. .!
  412. .!*****************************************************************************
  413. .!*                                          *
  414. .!*                End Part 6                      *
  415. .!*                                          *
  416. .!*****************************************************************************
  417.